package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevReportUrgeDao;
import com.sugon.entity.DevReportUrgeEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.DevReportUrgeService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报修催办记录Service实现类
 *
 * @author sugon
 * @date 2019-07-30 11:43:40
 */
@Service("devReportUrgeService")
public class DevReportUrgeServiceImpl implements DevReportUrgeService {
    @Autowired
    private DevReportUrgeDao devReportUrgeDao;
    @Autowired
    private SysMsgService sysMsgService;

    @Override
    public DevReportUrgeEntity queryObject(String REPAIRNO) {
        return devReportUrgeDao.queryObject(REPAIRNO);
    }

    @Override
    public List<DevReportUrgeEntity> queryList(Map<String, Object> map) {
        return devReportUrgeDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return devReportUrgeDao.queryTotal(map);
    }

    @Override
    public int save(String ticketId, String ticketType) {
        // 保存催办记录
        DevReportUrgeEntity devReportUrge = new DevReportUrgeEntity();
        devReportUrge.setId(RandomUtil.randomString(32));
        devReportUrge.setCreateUser(ShiroUtils.getUserEntity().getUserId());
        devReportUrge.setCreateTime(new Date());
        devReportUrge.setUrgentlyUser(ShiroUtils.getUserEntity().getUserId());
        devReportUrge.setUrgentlyTime(new Date());
        devReportUrge.setRepairNo(ticketId);
        devReportUrge.setTicketType(ticketType);
        devReportUrge.setDelFlag(1);
        devReportUrge.setDealStatus(1);
        return devReportUrgeDao.save(devReportUrge);
    }

    @Override
    public int save(DevReportUrgeEntity devReportUrge) {
        devReportUrge.setId(RandomUtil.randomString(32));
        devReportUrge.setCreateUser(devReportUrge.getUrgentlyUser());
        devReportUrge.setCreateTime(new Date());
        devReportUrge.setDelFlag(1);
        devReportUrge.setDealStatus(1);
        return devReportUrgeDao.save(devReportUrge);
    }

    @Override
    public int update(DevReportUrgeEntity devReportUrge) {
        // 更新催办后消息提醒
        SysMsgEntity msgEntity = new SysMsgEntity();
        msgEntity.setDataType("DEV_ZXGD");
        msgEntity.setDataUrl("/device/devmaintenwork.html");
        msgEntity.setTitle("设备维修工单");
        msgEntity.setType(SysMsgType.REPAIR_REMIND);
        msgEntity.setStartDate(new Date());
        msgEntity.setUserId(ShiroUtils.getUserId());
        sysMsgService.allMsgData(msgEntity);


        devReportUrge.setDealTime(new Date());
        devReportUrge.setDealUser(ShiroUtils.getUserId());
        devReportUrge.setUpdateTime(new Date());
        devReportUrge.setUpdateUser(ShiroUtils.getUserId());
        devReportUrge.setDealStatus(2);
        devReportUrge.setDelFlag(0);
        return devReportUrgeDao.update(devReportUrge);
    }

    @Override
    public int delete(String id) {
        return devReportUrgeDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return devReportUrgeDao.deleteBatch(ids);
    }

}
